package com.city.helper;


import com.city.domain.city.CityCopnotePerson;
import com.city.domain.house.FamliyDetails;
import com.city.domain.pub.PubFilepath;
import com.city.domain.pub.PubUser;
import com.city.domain.report.*;
import com.city.repository.report.CtUserBranchRepository;
import com.city.service.report.ReportService;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;

/**
 * @deprecation: 创建实例助手
 * @email: leichuqiao@126.com
 * @author: 雷楚桥
 * @date: 2018/7/23 17:18
 */
@Component
public class EntityHelper {

    @Resource
    private AuthHelper authHelper;
    private static AuthHelper authH;
    @Autowired
    private CtUserBranchRepository ctUserBranchRepository;




    @PostConstruct
    public void init() {
        authH = authHelper;
    }

    /**
     * @param categoryId Integer 上报类别
     * @param typeBig    Integer 案件分类[大类]
     * @param typeSmall  Integer 案件分类[小类]
     * @param title      Integer 案件标题[简介]
     * @param content    Integer 案件内容
     * @param townId     Integer 乡镇id
     * @param villageId  Integer 村庄id
     * @return CtReport
     * @deprecation 创建一个上报类
     * @author 雷楚桥
     * @date 2018/7/23 17:19
     */
    public CtReport createReport(Integer categoryId,
                                   Integer typeBig,
                                   Integer typeSmall,
                                   String title,
                                   String content,
                                   Integer townId,
                                   Integer villageId) {
        CtReport ctReport = new CtReport();

        ctReport.setTypeBig(typeBig);
        ctReport.setTypeSmall(typeSmall);
        ctReport.setTitle(title);
        ctReport.setContent(content);
        ctReport.setTownId(townId);
        ctReport.setVillageId(villageId);
//      ----------------------------------
        ctReport.setAddress("无");
        ctReport.setProvince("四川省");
        ctReport.setCity("宜宾市");
        ctReport.setCounty("兴文县");
        ctReport.setNatureId(1);
        ctReport.setSchedule(0);
        ctReport.setCategoryId(categoryId);

        //TODO 获取登陆信息

        ctReport.setReportId(authH.thisUserId());
        ctReport.setCreateTime(System.currentTimeMillis() / 1000 + "");
        return ctReport;
    }

    /**
     * @deprecation 创建一个上报类
     * @param json JSONObject 参数
     * @return CtReport
     * @author 雷楚桥
     * @date 2018-08-08 13:23
     * @modif 添加了设定taskId
     */
    public CtReport createReport(JSONObject json){
        CtReport rep = new CtReport();
        rep.setCategoryId((Integer)json.get("categoryId"));
        rep.setTownId((Integer)json.get("townId"));
        rep.setVillageId((Integer)json.get("villageId"));
        rep.setTypeBig((Integer)json.get("typeBig"));
        rep.setAddress((String)json.get("address"));
        if(!json.isNull("typeSmall"))
            rep.setTypeSmall((Integer)json.get("typeSmall"));
        rep.setTitle((String)json.get("title"));
        rep.setContent((String)json.get("content"));
        String ga[] = ((String)json.get("gis")).split(",");
        rep.setLng(ga[0]);
        rep.setLat(ga[1]);

        //--------------

        rep.setProvince("四川省");
        rep.setCity("宜宾市");
        rep.setCounty("兴文县");
        rep.setNatureId(1);
        rep.setSchedule(0);
        rep.setReportId(authH.thisUserId());
        rep.setCreateTime(System.currentTimeMillis() / 1000 + "");

        return rep;
    }


    /**
     * @param reportId Integer 督办案件id
     * @param comment String 督办备注
     * @param manId    Integer 督办人id
     * @deprecation 创建一条督办记录
     * @author 雷楚桥
     * @date 2018/7/30 10:51
     */
    public CtDoBan createDoBan(Integer reportId,String comment, Integer manId) {
        CtDoBan doBan = new CtDoBan();
        doBan.setReportId(reportId);
        doBan.setManId(manId);
        doBan.setComment(comment);
        doBan.setCreateTime(TimeStampHelper.getTimeStamp());
        return doBan;
    }


        /**
         * @deprecation 创建一个文件路径实体
         * @param tableName String 关联的表名
         * @param idIndex Integer 关联的主键
         * @param filePath String 文件路径
         * @param typeId Integer 上传文件后缀
         * @return PubFilepath
         * @author 雷楚桥
         * @date 2018-08-08 14:42
         */
    public PubFilepath createFilePath(String tableName,
                                      Integer idIndex,
                                      String filePath,
                                      Integer typeId) {
        PubFilepath filepath = new PubFilepath();
        filepath.setTableName(tableName);
        filepath.setIdIndex(idIndex+"");
        filepath.setFilePath(filePath);
        filepath.setTypeId(typeId);
        return filepath;
    }

    /**
     * @deprecation 创建一个文件
     * @param pName String 所属表名
     * @param pId Integer 所属表id
     * @param path String 文件路径
     * @return PubFilepath
     * @author 雷楚桥
     * @date 2018-08-08 14:42
     */
    public CtFile createFile(String pName, Integer pId, String path) {
        CtFile file = new CtFile();
        file.setpName(pName);
        file.setPId(pId);
        file.setPath(path);
        file.setExt(path.substring(path.lastIndexOf(".")));
        return file;
    }
    
    
    /**
     * @deprecation 创建一个督办记录
     * @param reportId Integer 案件id
     * @return CtDoBan
     * @author 雷楚桥
     * @date 2018-08-13 14:32
     */
    public CtDoBan createDoBan(Integer reportId) {
        CtDoBan doBan = new CtDoBan();
        doBan.setReportId(reportId);
        return doBan;
    }

    /**
     * @deprecation 创建一个回撤记录
     * @param reportId Integer 案件id
     * @param userId Integer 操作者id
     * @param remark String 备注
     * @return CtBack
     * @author 雷楚桥
     * @date 2018-08-22 16:10
     */
    public CtBack createBack(Integer reportId, Integer userId,String remark) {
        CtBack ctBack = new CtBack();
        ctBack.setReportId(reportId);
        ctBack.setUserId(userId);
        ctBack.setRemark(remark);
        ctBack.setCreateTime(TimeStampHelper.getTimeStamp());
        return ctBack;
    }

    /**
     * @deprecation 创建一个民情日记对象
     * @param json JSONObject 参数
     * @return CityCopnote
     * @author 雷楚桥
     * @date 2018-08-24 10:49
     */
    public CtCopnote createCopnote(JSONObject json) {
        System.out.println(json);
        CtCopnote copnote = new CtCopnote();
        copnote.setReportId(authH.thisUserId());
        copnote.setTownId((Integer)json.get("townId"));
        copnote.setVillageId((Integer)json.get("villageId"));
        copnote.setAddress((String)json.get("address"));
        String ga[] = ((String)json.get("gis")).split(",");
        copnote.setLng(ga[0]);
        copnote.setLat(ga[1]);
        copnote.setMainObjectId((Integer)json.get("mainObjectId"));
        copnote.setMainContentId((Integer)json.get("mainContentId"));
        copnote.setComment((String)json.get("comment"));
        copnote.setSchedule(0);
        copnote.setCreateTime(TimeStampHelper.getTimeStamp());
        return copnote;
    }

    /**
     * @deprecation 创建民情日记采访对象
     * @param json JSONObject json对象
     * @return CityCopnotePerson
     * @author 雷楚桥
     * @date 2018-08-24 15:27
     */
    public CityCopnotePerson createCopnotePerson(JSONObject json) {
        CityCopnotePerson person = new CityCopnotePerson();
        person.setName((String)json.get("name"));
        person.setFamilyPnum((Integer)json.get("familyPnum"));
        person.setEconomicsIncome((Integer)json.get("economicsIncome"));
        person.setPersonType((Integer)json.get("personType"));
        person.setContent((String)json.get("content"));
        person.setExperience((String)json.get("experience"));
        person.setHouseStatus((String)json.get("houseStatus"));
        person.setTime(TimeStampHelper.getTimeStamp());
        return person;
    }

    /**
     * @deprecation 创建一个user实例
     * @param json JSONObject json参数
     * @return PubUser
     * @author 雷楚桥
     * @date 2018-08-24 16:32
     */
    public PubUser createUser(JSONObject json) {
        PubUser user = new PubUser();
        user.setTownId((Integer)json.get("townId"));
        user.setVillageId((Integer)json.get("villageId"));
        user.setName((String)json.get("name"));
        user.setIdCard((String)json.get("idCard"));
        user.setAge((Integer)json.get("age"));
        user.setBorn((String)json.get("born"));
        user.setHrResiter((String)json.get("hrResiter"));
        user.setBornAddress((String)json.get("bornAddress"));
        user.setMarriage((String)json.get("marriage"));
        user.setEducation((String)json.get("education"));
        user.setSchool((String)json.get("school"));
        user.setTellphone((String)json.get("tellphone"));
        user.setSex((Integer)json.get("sex"));
        user.setNation((Integer)json.get("nation"));
        user.setIsLdl((Integer) json.get("isLdl"));
        user.setIsCj((Integer) json.get("isCj"));
        user.setHealthStatus((Integer) json.get("healthStatus"));
        user.setIsDb((Integer) json.get("isDb"));
        user.setIsBx((Integer) json.get("isBx"));
        user.setIsWb((Integer) json.get("isWb"));
        user.setWork((String) json.get("work"));
        user.setIndustry((String) json.get("industry"));
        user.setMajor((String) json.get("major"));
        user.setEmail((String) json.get("email"));
        user.setQq((String) json.get("qq"));
        user.setWeixin((String) json.get("weixin"));
        user.setLivein((String) json.get("livein"));
        user.setRelation(0);
        return user;
    }

    /**
     * @Author: 李顺时
     * @Description: 创建一个法定代表人实例
     * @Date: 16:41 2018/9/7
     */
    public CtLegalPerson createLegalPerson(JSONObject json) {
        CtLegalPerson legalPerson = new CtLegalPerson();
        legalPerson.setCompanyName((String)json.get("companyName"));
        legalPerson.setEmail((String)json.get("email"));
        legalPerson.setIdCard((String)json.get("idCard"));
        legalPerson.setName((String)json.get("name"));
        legalPerson.setPosition((String)json.get("position"));
        legalPerson.setTelphone((String)json.get("telphone"));
        return legalPerson;
    }

    /**
     * @Author: 李顺时
     * @Description: 创建一个地理空间实例
     * @Date: 16:41 2018/9/7
     */
    public CtGeospatial createGeospatial(JSONObject json) {
        CtGeospatial ctGeospatial = new CtGeospatial();
        ctGeospatial.setAddress((String)json.get("address"));
        ctGeospatial.setEventDescribe((String)json.get("eventDescribe"));
        ctGeospatial.setEventType((String)json.get("eventType"));
        ctGeospatial.setGathererIdCard((String)json.get("gathererIdCard"));
        ctGeospatial.setGathererName((String)json.get("gathererName"));
        ctGeospatial.setLat((String)json.get("lat"));
        ctGeospatial.setLng((String)json.get("lng"));
        ctGeospatial.setReCall((String)json.get("reCall"));
        ctGeospatial.setTown((String)json.get("town"));
        ctGeospatial.setVillage((String)json.get("village"));

        //        ---默认
        ctGeospatial.setProvince("51");
        ctGeospatial.setCity("5115");
        ctGeospatial.setCounty("511528");


        return ctGeospatial;
    }

    /**
     * @Author: 李顺时
     * @Description: 创建一个农地数据实例
     * @Date: 16:42 2018/9/7
     */
    public CtCropland createCropland(JSONObject json) {
        CtCropland ctCropland = new CtCropland();
        ctCropland.setAddress((String)json.get("address"));
        ctCropland.setEventDescribe((String)json.get("eventDescribe"));
        ctCropland.setEventType((String)json.get("eventType"));
        ctCropland.setGathererIdCard((String)json.get("gathererIdCard"));
        ctCropland.setGathererName((String)json.get("gathererName"));
        ctCropland.setLat((String)json.get("lat"));
        ctCropland.setLng((String)json.get("lng"));
        ctCropland.setReCall((String)json.get("reCall"));
        ctCropland.setTown((String)json.get("town"));
        ctCropland.setVillage((String)json.get("village"));
        ctCropland.setLandArea((String)json.get("landArea"));
        ctCropland.setLandPresentSituation((String)json.get("landPresentSituation"));
        ctCropland.setLandType((String)json.get("landType"));

        //        ---默认
        ctCropland.setProvince("51");
        ctCropland.setCity("5115");
        ctCropland.setCounty("511528");


        return ctCropland;
    }


    /**
     * @Author: 李顺时
     * @Description: 创建一个建筑物实例
     * @Date: 16:42 2018/9/7
     */
    public CtBuild createBuild(JSONObject json) {
        CtBuild ctBuild = new CtBuild();
        ctBuild.setAddress((String)json.get("address"));
        ctBuild.setEventDescribe((String)json.get("eventDescribe"));
        ctBuild.setEventType((String)json.get("eventType"));
        ctBuild.setGathererIdCard((String)json.get("gathererIdCard"));
        ctBuild.setGathererName((String)json.get("gathererName"));
        ctBuild.setReCall((String)json.get("reCall"));
        ctBuild.setTown((String)json.get("town"));
        ctBuild.setVillage((String)json.get("village"));
        ctBuild.setBuildDescribe((String)json.get("buildDescribe"));
        ctBuild.setBuildName((String)json.get("buildName"));
        ctBuild.setBuildType((String)json.get("buildType"));
        ctBuild.setFloor(Integer.parseInt((String)json.get("floor")));
        ctBuild.setLng((String)json.get("lng"));
        ctBuild.setLat((String)json.get("lat"));

//        ---默认
        ctBuild.setProvince("51");
        ctBuild.setCity("5115");
        ctBuild.setCounty("511528");
        ctBuild.setCode(System.currentTimeMillis() / 1000 + "");

        return ctBuild;
    }

    /**
     * @Author: 李顺时
     * @Description: 创建一个部件实例
     * @Date: 16:42 2018/9/7
     */
    public CtPart createPart(JSONObject json) {
        CtPart ctPart = new CtPart();
        ctPart.setAddress((String)json.get("address"));
        ctPart.setCompetentOrg((String)json.get("competentOrg"));
        ctPart.setLat((String)json.get("lat"));
        ctPart.setLng((String)json.get("lng"));

        ctPart.setPartDescribe((String)json.get("partDescribe"));
        ctPart.setPartName((String)json.get("partName"));
        ctPart.setPartStatus(Integer.parseInt((String)json.get("partStatus")));
        ctPart.setPowerUnit((String)json.get("powerUnit"));
        ctPart.setTypeBig((String)json.get("typeBig"));
        ctPart.setTypeSmall((String)json.get("typeSmall"));


        ctPart.setPartCode(System.currentTimeMillis() / 1000 + "");

        return ctPart;
    }


    /**
     * @deprecation 创建一个消息实例
     * @param type String 消息类型
     * @param acceptId Integer 接受者id
     * @param content String 发送内容
     * @param remarks String 备注[需要的json信息]
     * @return CtMessage
     * @author 雷楚桥
     * @date 2018-09-12 2:37
     */
    public CtMessage createMessage(String type, Integer acceptId, String content, String remarks) {

        CtMessage message = new CtMessage();
        message.setType(type);
        message.setSendId(authHelper.thisUserId());
        message.setAcceptId(acceptId);
        message.setContent(content);
        message.setRemarks(remarks);
        message.setCreateTime(TimeStampHelper.getTimeStamp());
        return message;
    }


    /**
     * @deprecation 创建一个消息返回实例
     * @param message CtMessage 开始的消息
     * @param info String 返回消息
     * @return CtMessage
     * @author 雷楚桥
     * @date 2018-09-12 16:43
     */
    public CtMessage createMessage(CtMessage message, String info,Integer taskId) {
        CtMessage msg = new CtMessage();
        msg.setType(message.getType());
        msg.setSendId(authHelper.thisUserId());
        msg.setAcceptId(ctUserBranchRepository.findBranchIdByUserId(message.getSendId()));
        if(info.equals("请求同意")){
            msg.setStatus(1);
        }else if(info.equals("请求拒绝")){
            msg.setStatus(0);
        }
        msg.setContent(info);
        msg.setRemarks(message.getRemarks());
        msg.setTaskId(taskId);
        SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        msg.setCreateTime(sfd.format(new Date()));
        return msg;
    }

    public FamliyDetails createFamliyDetails(PubUser p){
        PubUserJudgeUtils pubUserJudgeUtils = new PubUserJudgeUtils();
        FamliyDetails famliyDetails = new FamliyDetails();
        String nationName;
        String relation;
        String sex;
        String industry;
        if(p.getNation()!=null){
            nationName=pubUserJudgeUtils.getNationName(Integer.parseInt(p.getNation()+""));
        }else{
            nationName = "";
        }
        if(p.getRelation()!=null){
            relation= pubUserJudgeUtils.getRelation(p.getRelation());
        }else{
            relation = "";
        }
        if(p.getSex()!=null){
            sex= pubUserJudgeUtils.getSex(p.getSex());
        }else {
            sex = "";
        }
        if(p.getIndustry()!=null){
            industry= pubUserJudgeUtils.getIndustryName(Integer.parseInt(p.getIndustry()));
        } else{
            industry="";
        }


        famliyDetails.setId(p.getId());
        famliyDetails.setAge(p.getAge());
        famliyDetails.setBorn(p.getBorn());
        famliyDetails.setBornAddress(p.getBornAddress());
        famliyDetails.setEducation(p.getEducation());
        famliyDetails.setEmail(p.getEmail());
        famliyDetails.setHealthStatus(pubUserJudgeUtils.checkHealthStatus(p.getHealthStatus()));//健康状态
        famliyDetails.setHouseholdId(p.getHouseholdId());
        famliyDetails.setHrResiter(p.getHrResiter());
        famliyDetails.setIdCard(p.getIdCard());
        famliyDetails.setIncome(p.getIncome());
        famliyDetails.setIsBx(pubUserJudgeUtils.checkIsBx(p.getIsBx()));//保险
        famliyDetails.setIsCj(pubUserJudgeUtils.checkIsCj(p.getIsCj()));//残疾
        famliyDetails.setIsDb(pubUserJudgeUtils.checkIsDb(p.getIsDb()));//低保
        famliyDetails.setIsLdl(pubUserJudgeUtils.checkIsLdl(p.getIsLdl()));//劳动力
        famliyDetails.setIsWb(pubUserJudgeUtils.checkIsWb(p.getIsWb()));//五保
        famliyDetails.setLivein(p.getLivein());
        famliyDetails.setMajor(p.getMajor());
        famliyDetails.setMarriage(p.getMarriage());
        famliyDetails.setName(p.getName());
        famliyDetails.setNohRegister(p.getNohRegister());
        famliyDetails.setPoliticalOutlook(pubUserJudgeUtils.getPoliticalOutlook(p.getPoliticalOutlook()));
        famliyDetails.setQq(p.getQq());
        famliyDetails.setTellphone(p.getTellphone());
        famliyDetails.setSchool(p.getSchool());
        famliyDetails.setTownId(p.getTownId());
        famliyDetails.setUpdateTime(p.getUpdateTime());
        famliyDetails.setVillageId(p.getVillageId());
        famliyDetails.setWeixin(p.getWeixin());
        famliyDetails.setWork(pubUserJudgeUtils.getWorkStatus(p.getWork()));
        famliyDetails.setNation(nationName);
        famliyDetails.setRelation(relation);
        famliyDetails.setSex(sex);
        famliyDetails.setIndustry(industry);
        famliyDetails.setBeizhu(pubUserJudgeUtils.beizhu(p));



        return famliyDetails;
    }

}
